package com.fjwt.gz.proxy.ctrl.home;

import com.alibaba.fastjson2.JSONObject;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.dto.StatMemberNumDTO;
import com.fjwt.gz.db.entity.StatScoreNumDayEntity;
import com.fjwt.gz.db.vo.StatMemberNumVO;
import com.fjwt.gz.proxy.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.StatMemberNumService;
import com.fjwt.gz.service.impl.StatScoreNumDayService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 *  首页统计
 * @author myf
 */
@RestController
@RequestMapping("/api/homePageStatistics")
@Slf4j
public class HomePageStatisticsController extends CommonCtrl {

    @Autowired
    private StatScoreNumDayService statScoreNumDayService;
    @Autowired
    private StatMemberNumService statMemberNumService;

    /**
     * 数据总览统计'
     * @return
     */
    @PostMapping("/getDataOverviewStatistics")
    public ApiRes getDataOverviewStatistics() {
        StatMemberNumDTO statMemberNumDTO = getObject(StatMemberNumDTO.class);
        JSONObject result = new JSONObject();
        String currentAgentNo = getCurrentAgentNo();
        StatScoreNumDayEntity statScoreNumDayEntity = statScoreNumDayService.getOverviewStatisticsByAgentNo(currentAgentNo);
        result.put("unusedScore", statScoreNumDayEntity.getTotalUnusedScoreNum());
        result.put("usedScore", statScoreNumDayEntity.getTotalUsedScoreNum());
        statMemberNumDTO.setAgentNo(currentAgentNo);
        List<StatMemberNumVO> agentDetails = statMemberNumService.getBaseMapper().getAgentDetails(statMemberNumDTO);
        result.put("totalMemberNum", ObjectUtils.isEmpty(agentDetails) || ObjectUtils.isEmpty(agentDetails.get(0)) ? 0 : agentDetails.get(0).getTotalMemberNum());
        result.put("totalCstmMemberNum", ObjectUtils.isEmpty(agentDetails) || ObjectUtils.isEmpty(agentDetails.get(0)) ? 0 : agentDetails.get(0).getTotalCstmMemberNum());
        return ApiRes.ok(result);
    }

    /**
     * 机构发展客户数据统计'
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ENT_C_MAIN_OVERVIEW_STATS')")
    @PostMapping("/getAgentMemberNumStatistics")
    public ApiRes getAgentMemberNumStatistics() {
        StatMemberNumDTO statMemberNumDTO = getObject(StatMemberNumDTO.class);
        statMemberNumDTO.setAgentNo(getCurrentAgentNo());
        return ApiRes.ok(statMemberNumService.getMemberNumStatistics(statMemberNumDTO));
    }

}
